<?php
namespace Home\Controller;
use Common\Controller\BaseController;

class UploadController extends BaseController{
	//定义允许上传的文件扩展名
	private $ext_arr = array(
//				'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
//				'flash' => array('swf', 'flv'),
//				'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
//				'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
				'image' => array('gif', 'jpg', 'jpeg', 'png'),
				'flash' => array('swf', 'flv'),
				'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
				'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt'),
			);

	/*
	 * Uploadify文件上传组件的C层
	 */
	public function index(){
		$path = '/Public/Upload/' . date('Ymd') . '/';
		if(!file_exists($_SERVER["DOCUMENT_ROOT"] . $path)){
			mkdir($_SERVER["DOCUMENT_ROOT"] . $path, 0777, true);
			chmod($_SERVER["DOCUMENT_ROOT"] . $path, 0777);
		}
		$file_name = $_FILES['file']['name'];
		$fileName = $path . date('His') . mt_rand(100, 999) . strrchr($file_name, '.');
		move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER["DOCUMENT_ROOT"] . $fileName);
		exit('{"error":0, "url": "' . $fileName . '"}');
	}

	/**
	 * keditor编辑器中处理文件上传的C层
	 */
	public function bjqupload(){
		$path = '/Public/Upload/' . date('Ymd') . '/';
		if(!file_exists($_SERVER["DOCUMENT_ROOT"] . $path)){
			mkdir($_SERVER["DOCUMENT_ROOT"] . $path, 0777, true);
			chmod($_SERVER["DOCUMENT_ROOT"] . $path, 0777);
		}
		//PHP上传失败
		if (!empty($_FILES['file']['error'])) {
			switch($_FILES['file']['error']){
				case '1':
					$error = '超过系统允许的大小。';
					break;
				case '2':
					$error = '超过表单允许的大小。';
					break;
				case '3':
					$error = '只有部分被上传。';
					break;
				case '4':
					$error = '请选择文件。';
					break;
				case '6':
					$error = '找不到临时目录。';
					break;
				case '7':
					$error = '写文件到硬盘出错。';
					break;
				case '8':
					$error = 'File upload stopped by extension。';
					break;
				case '999':
				default:
					$error = '未知错误。';
			}
			exit('{"error":1, "message": "' . $error . '"}');
		}

		//文件标识
		$dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
		if (empty($this->ext_arr[$dir_name])) {
			$error = "请正常使用";
			exit('{"error":1, "message": "' . $error . '"}');
		}
		$file_name = $_FILES['file']['name'];

		//获得文件扩展名
		$temp_arr = explode(".", $file_name);
		$file_ext = array_pop($temp_arr);
		$file_ext = trim($file_ext);
		$file_ext = strtolower($file_ext);
		//检查扩展名
		if (in_array($file_ext, $this->ext_arr[$dir_name]) === false) {
			$error = "上传文件扩展名是不允许的扩展名。只允许" . implode(",", $this->ext_arr[$dir_name]) . "格式。";
			exit('{"error":1, "message": "' . $error . '"}');
		}

		$fileName = $path . date('His') . mt_rand(100, 999) . strrchr($file_name, '.');
		move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER["DOCUMENT_ROOT"] . $fileName);
		exit('{"error":0, "url": "' . $fileName . '"}');

	}
}
?>